/*
 * Heap2UI.cpp
 *
 *  Created on: 15-Nov-2009
 *
 */

#include "../DiskSpaceManager/Database.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
#include<string.h>
#include <malloc.h>
#include <vector>
#include <time.h>
#include "Heap.h"
#include <strings.h>
#include "../common.h"
#include <stdlib.h>

namespace HeapGUI{
int initHeap();
};

int HeapGUI::initHeap(){

	enum database{EXIT, CREATE, OPEN,INSERT, DELETEDATA, UPDATEDATA, GETDATA, GETALLDATA,COUNT,DELETEHEAP,VERBOSE};
		Heap2 hp(0);
		Status st = ok;
		int file = -1;
		char* data = "hello world i am in iiitb doing M.Tech from here this place is kool Officially this is a good time of";
		char* datanew = "i m in iiitb";
		char* data3 = "doing M.Tech from here";
		char* data4 = "this place is kool";
		char* data5 = "new one";
		char* update = "updated data";
		unsigned char* rec;
		PageId pid;
		int input = -1;

	while(true){

		printf("\n\n1 - Create Heap File\n");
		printf("2 - Open Heap File\n");
		printf("3 - Insert Data\n");
		printf("4 - Delete Data\n");
		printf("5 - Update Data\n");
		printf("6 - Get Data\n");
		printf("7 - Get All Data\n");
		printf("8 - Get Count of records\n");
		printf("9 - Delete Heap\n");
		printf("10 - Verbose setting\n");
		printf("0 - Exit Program\n\n");
		cout << endl << endl;
		printf("Enter the choice : ");
		scanf("%d",&input);
		switch(input){

		case EXIT:
			cout << "Program Terminated\n";
			return 0;

		case CREATE: //TODO here there should be a creation of heap file the starting header file will be passed from upper UI

			file = hp.createHeapFile();
			cout<<"\nHeap File Created In Page No.: "<<file<<endl;
			break;

		case OPEN:
			cout<<"Enter The First Dp Page Number of Heap File to Open: ";
			cin>>file;
			break;

		case INSERT:
			int rand;
			if(file == -1)
			{
				cout<<"Open/Create file first";
				break;
			}
			long count;
			printf("Enter the Number of records you want to enter: ");
			cin>>count;
			yadbms_common::rid ridd;
			rand=0;
			for (long iter = 0; iter <count; iter++) {
				//if(rand==0){
					hp.insertRecord(file,(unsigned char*)data,(int)strlen(data),ridd);
					//rand++;
				//}
				//else if(rand==1){
				//	hp.insertRecord(file,(unsigned char*)data4,(int)strlen(data),ridd);
				//	rand=0;
				//}

			}
		break;

		case DELETEDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first\n";
				break;
			}

			printf("\nEnter the Record Id of the record you want to delete ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			hp.deleteRecord(file,pid);
		break;

		case UPDATEDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first\n";
				break;
			}

			printf("\nEnter the Record Id of the record you want to update ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			hp.updateRecord(file,pid,(unsigned char*)datanew,strlen(datanew));
		break;

		case GETDATA:
			if(file == -1)
			{
				cout<<"Open/Create file first\n";
				break;
			}
			printf("\nEnter the Record Id of the record you want to get ---->\n\n");
			printf("Enter Page Number: ");
			scanf("%d",&pid.pageno);
			printf("Enter Slot Number: ");
			scanf("%d",&pid.slotno);
			int len;
			st = hp.getRecord(file,pid,rec,len);

			if(st != ok) cout << "no record exist at this page and slot \n";
			else if(rec != NULL){
				cout << "\n\nRecord found :\t";
				 for(int iter=0; iter<len; iter++){
					 cout << *(rec + iter) ;
				 }
			}
			cout << "\n\n\n";
		break;

		case GETALLDATA:{
			if(file == -1)
			{
				cout<<"Open/Create file first\n";
				break;
			}

			vector<record> resultset;
			hp.openScan(file,resultset);
			int count=0;
			cout<<"\nNumber of record: "<<resultset.size()<<"\n";
			for(unsigned int i=0;i<resultset.size();i++)
			{
				//cout << "Length of data is :" << resultset[i].recLen << endl;
				count++;
				cout << "Record " << (i+1) << ":";
				for(unsigned int j=0;j<resultset[i].recLen;j++){
					cout<<resultset[i].rec[j];
				}
				cout << endl;
				if(count>20) {getchar(); count=0;}
			}
		break;
		}

		case COUNT:
			if(file == -1)
			{
				cout<<"Open/Create file first\n";
				break;
			}
			cout<<"\nNumber of record: "<<hp.getRecCnt(file)<<"\n";
			break;

		case DELETEHEAP:
			int file;
			Status st;
			cout << "Enter the first Dp page of Heap you want to delete :" << endl;
			cin >> file;
			st = hp.deleteHeap(file);
			if(st==error)
				cout << "Error while deleting" << endl;
			else if(st==ok)
				cout << "Heap file deleted successfully" << endl;
			break;

		case VERBOSE:
			hp.set_debug();
		break;
		default:
			cout << "invalid input\n";
		break;
		};

	};

}




